package com.zwy.stack.impl;

import com.zwy.linear.SLNode;
import com.zwy.stack.Queue;
/*
    链队列
 */
public class QueueSLinked implements Queue {
   private SLNode front;
   private SLNode rear;
   private int size;

    public QueueSLinked() {
        front=new SLNode();
        rear=front;
        size=0;
    }

    @Override
    public int getSize() {
        return size;
    }

    @Override
    public boolean isEmpty() {
        return size==0;
    }

    @Override
    public void enqueue(Object e) {
        SLNode p=new SLNode(e,null);
        rear.setNext(p);
        rear=p;
        size++;
    }

    @Override
    public Object dequeue() {
        if(size<1){
            throw new RuntimeException("队列空");
        }
        SLNode p=front.getNext();
        front.setNext(p.getNext());
        size--;
        if(size<1)rear=front;
        return p.getData();
    }

    @Override
    public Object peek() {
        if(size<1){
            throw new RuntimeException("队列空");
        }
        return front.getNext().getData();
    }
}
